﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Text;

namespace BarleyBreak
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            Run();
        }

        private Engine m_engine = new Engine();
        private Canvas[] m_canvas = new Canvas[9];


        public void Run()
        {
            m_engine.Init(this);

            for (int i = 0; i < 9; ++i)
            {
                Cell cell = m_engine.GetCell(i);
                Image image = cell.GetImage();
                m_canvas[i] = new Canvas();
                m_canvas[i].Width = Engine.m_scale;
                m_canvas[i].Height = Engine.m_scale;
                m_canvas[i].Children.Add(image);
                m_canvas[i].SetValue(Canvas.NameProperty, "Cell" + i.ToString());
                m_canvas[i].MouseLeftButtonDown += new MouseButtonEventHandler(MouseLeftButtonDown);

                m_canvas[i].SetValue(Canvas.TopProperty, Convert.ToDouble(cell.Y * Engine.m_scale));
                m_canvas[i].SetValue(Canvas.LeftProperty, Convert.ToDouble(cell.X * Engine.m_scale));

                CanvasMain.Children.Add(m_canvas[i]);
            }
        }


        private void MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Canvas can = sender as Canvas;
            for (int i = 0; i < Engine.m_cellsCount; ++i)
            {
                if (can == m_canvas[i])
                {
                    m_engine.Passage(i);
                    if (GameOver())
                    {
                        ShowGameOver();
                    }
                }
            }
        }

        public void MoveCell(int currentPosition, int xNew, int yNew)
        {
            Cell cell1 = m_engine.GetCell(currentPosition);
            int x1 = cell1.X;
            int y1 = cell1.Y;

            Cell cell2 = m_engine.GetCell(xNew, yNew);
            int cellPosition = cell2.Position;

            // update position in the 2 cells
            cell1.SetCoordinates(xNew, yNew);
            cell2.SetCoordinates(x1, y1);

            // update view of 2 cells
            m_canvas[currentPosition].SetValue(Canvas.TopProperty, Convert.ToDouble(cell1.Y * Engine.m_scale));
            m_canvas[currentPosition].SetValue(Canvas.LeftProperty, Convert.ToDouble(cell1.X * Engine.m_scale));

            m_canvas[cellPosition].SetValue(Canvas.TopProperty, Convert.ToDouble(cell2.Y * Engine.m_scale));
            m_canvas[cellPosition].SetValue(Canvas.LeftProperty, Convert.ToDouble(cell2.X * Engine.m_scale));

            CanvasMain.InvalidateArrange();
        }

        private bool GameOver()
        {
            return m_engine.GameOver();
        }

        private void ShowGameOver()
        {
            MessageBox.Show("Congradulations!!! You winner!!! Game over!!!");
        }

        private void ImageFailed(object sender, ExceptionRoutedEventArgs args)
        {
            MessageBox.Show(args.ErrorException.Message);
        }
    }
}
